package com.exm.sql.service;


import com.alibaba.fastjson2.JSON;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exm.sql.config.RedisKeyDic;
import com.exm.sql.config.TokenContext;
import com.exm.sql.model.User;
import com.exm.sql.model.vo.ResponseResult;
import com.exm.sql.repository.UserRepository;
import com.exm.sql.util.JwtUtils;
import com.exm.sql.util.RedisUtils;
import com.exm.sql.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Service
public class UserSerivce  {



    @Autowired
    UserRepository userRepository;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    UserUtil userUtil;

    public List<User> getUserName() {
       return userRepository.list();
    }

    // 查询所有用户
    public List<User> getAllUsers() {
        return userRepository.list();
    }

    // 根据 ID 查询用户
    public User getUserById(Long id) {
        return userRepository.getById(id);
    }


    public ResponseResult doLogin(String account, String password) {
        User user = userRepository.getUserByAccount(account);
        if (user == null) {
            return ResponseResult.error("未查询到此用户");
        }else {
            if (user.getPassword().equals(password)) {
                HashMap<String,String> userMap = new HashMap<>();
                userMap.put("account", account);
                userMap.put("password", password);
                String token = JwtUtils.getToken(userMap);
                String userJson = JSON.toJSONString(user);
                redisUtils.set(RedisKeyDic.USER_TOKEN.getFormattedKey(token),userJson,3 * 24 * 60 * 60);
                return ResponseResult.success(token);
            }else {
                return ResponseResult.error("账号密码错错误");
            }
        }
    }

    public ResponseResult doRegister(User user) {
        long count = userRepository.getUserCountByAccount(user.getAccount());
        if (count > 0) {
            return ResponseResult.error("已存在用户名");
        }else {
            boolean save = userRepository.save(user);
            if (save) {
                return doLogin(String.valueOf(user.getAccount()),user.getPassword());
            }else {
                return ResponseResult.error("注册失败");
            }
        }

    }

    public ResponseResult getUserInfo(){
        String account= userUtil.getUserAccountInfoByToken();
        User userByAccount = userRepository.getUserByAccount(account);
        if (userByAccount!=null){
            return ResponseResult.success(userByAccount);
        }else {
            return ResponseResult.error();
        }

    }


    public ResponseResult save(User newUser){
        String account= userUtil.getUserAccountInfoByToken();
        newUser.setPassword(null);
        newUser.setAccount(account);
        Boolean aBoolean = userRepository.setUserByAccount(newUser);
        if (aBoolean) {
            return ResponseResult.success();
        }else {
            return ResponseResult.error();
        }




    }





    
}
